package com.tanhua.dubbo.api;

import com.tanhua.model.mongo.Friend;
import com.tanhua.model.mongo.UserLike;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@DubboService
public class FriendApiImpl implements FriendApi{
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void save(Long userId, Long friendId) {
        //1、保存自己的好友数据
        Query query1 = Query.query(Criteria.where("userId").is(userId).and("frinedId").is(friendId));
        //1.1 判断好友关系是否存在
        if(!mongoTemplate.exists(query1, Friend.class)) {
            //1.2 如果不存在，保存
            Friend friend1 = new Friend();
            friend1.setUserId(userId);
            friend1.setFriendId(friendId);
            friend1.setCreated(System.currentTimeMillis());
            mongoTemplate.save(friend1);
        }
        //2、保存好友的数据
        Query query2 = Query.query(Criteria.where("userId").is(friendId).and("frinedId").is(userId));
        //2.1 判断好友关系是否存在
        if(!mongoTemplate.exists(query2, Friend.class)) {
            //2.2 如果不存在，保存
            Friend friend1 = new Friend();
            friend1.setUserId(friendId);
            friend1.setFriendId(userId);
            friend1.setCreated(System.currentTimeMillis());
            mongoTemplate.save(friend1);
        }

    }

    @Override
    public List<Friend> listById(Long userId,int page, int pagesize) {
        Query query=Query.query(Criteria.where("userId").is(userId))
                .skip((page-1)*pagesize)
                .limit(pagesize)
                .with(Sort.by(Sort.Order.desc("created")));
        return mongoTemplate.find(query,Friend.class);
    }

    @Override
    public Integer findCountMatching(Long id) {
        Query query = Query.query(Criteria.where("userId").is(id));
        Long count = mongoTemplate.count(query, Friend.class);
        return Integer.valueOf(count.toString());
    }

    @Override
    public Integer findBeLiked(Long id) {
        Query query = Query.query(Criteria.where("likeUserId").is(id).and("isLike").is(true));
        Long count = mongoTemplate.count(query, UserLike.class);
        return Integer.valueOf(count.toString());
    }

    @Override
    public Integer findLiked(Long id) {
        Query query = Query.query(Criteria.where("userId").is(id).and("isLike").is(true));
        Long count = mongoTemplate.count(query, UserLike.class);
        return Integer.valueOf(count.toString());
    }
}
